class Invoice < ActiveRecord::Base
  belongs_to            :order
  has_many              :line_items
  validates_presence_of :order
  validates_presence_of :customer
  
  after_destroy         :destroy_dependents
  
  def grand_total
    grand_total = 0
    self.line_items.each {|line_item| grand_total = grand_total + line_item.cost}
    return grand_total
  end
protected
  def destroy_dependents
    self.line_items.each {|line_item| line_item.destroy} unless self.line_items.empty?
    self.order.destroy if self.order
  end
end
